﻿using Jasmine.API.Models;
using Jasmine.Extensions.Decimals;
using Jasmine.Extensions.Doubles;
using Jasmine.Extensions.Floats;
using Jasmine.Extensions.Lists;
using Jasmine.Extensions.Tables;
using Microsoft.AspNetCore.Mvc;
using System.Data;

namespace Jasmine.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController : ControllerBase
    {

        [HttpGet]
        public ActionResult Decimal(decimal d)
        {
            d = d > 0 ? d : 12345.6789M;
            var temp = d.ToFixed(2);
            return Ok(temp);
        }

        [HttpGet]
        public ActionResult Double(double d)
        {
            d = d > 0 ? d : 12345.6789D;
            var temp = d.ToFixed(2);
            return Ok(temp);
        }

        [HttpGet]
        public ActionResult Float(float d)
        {
            d = d > 0 ? d : 12345.6789F;
            var temp = d.ToFixed(2);
            return Ok(temp);

        }

        [HttpGet]
        public ActionResult ListAdd()
        {
            var list = new List<int> { 1, 2, 3, 4, 5 };
            list.AddRangeIfNotExists([4, 5, 6, 7, 8]);
            return Ok(list);
        }

        [HttpGet]
        public ActionResult ListToTable()
        {
            List<Person> people =
            [
                new Person { Name = "Alice", Age = 30 },
                new Person { Name = "Bob", Age = 25 },
                new Person { Name = "Charlie", Age = 35 }
            ];
            var table = people.ToDataTable();
            return Ok(table.ToList<Person>());
        }

        [HttpGet]
        public ActionResult TableToList()
        {
            DataTable table = new();
            table.Columns.Add(nameof(Person.Name), typeof(String));
            table.Columns.Add(nameof(Person.Age), typeof(int));
            var row1 = table.NewRow();
            row1[nameof(Person.Name)] = "Alice";
            row1[nameof(Person.Age)] = 30;
            table.Rows.Add(row1);
            var row2 = table.NewRow();
            row2[nameof(Person.Name)]= "Bob";
            row2[nameof(Person.Age)] = 25;
            table.Rows.Add(row2);
            var row3 = table.NewRow();
            row3[nameof(Person.Name)] = "Charlie";
            row3[nameof(Person.Age)] = 35;
            table.Rows.Add(row3);

            return Ok(table.ToList<Person>());
        }

    }
}
